package com.schoolMall.service.impl;

import com.schoolMall.dao.ProductImgDao;
import com.schoolMall.domain.ProductImg;
import com.schoolMall.dto.ProductImgDto;
import com.schoolMall.enums.ProductImgStateEnum;
import com.schoolMall.exception.ProductOperationException;
import com.schoolMall.service.ProductImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author:zyh
 * @Time:2021-09-01-9:59
 * @email:1269231889@qq.com
 */
public class ProductImgServiceImpl implements ProductImgService {
    @Autowired
    private ProductImgDao productImgDao;

    @Override
    public ProductImgDto batchInsertProductImg(List<ProductImg> productImgList) throws ProductOperationException {
        if (CollectionUtils.isEmpty(productImgList)) {
            return new ProductImgDto(ProductImgStateEnum.NULL_IMG);
        }
        List<ProductImg> productImgs = productImgList.stream()
                .filter(productImg -> Objects.nonNull(productImg.getProductId())).collect(Collectors.toList());
        int insertNum = 0;
        try {
            insertNum = productImgDao.batchInsertImg(productImgs);
        } catch (Exception e) {
            throw new ProductOperationException("插入异常");
        }
        if (insertNum < 1) {
            throw new ProductOperationException("插入失败");
        }
        return new ProductImgDto(ProductImgStateEnum.SUCCESS);
    }
}
